//
//  AICommonWebController.m
//  Mia
//
//  Created by Vill on 2024/1/18.
//

#import "AICommonWebController.h"
#import <WebKit/WebKit.h>

@interface AICommonWebController ()<WKNavigationDelegate>

@property (nonatomic , weak) WKWebView *webView;
@property (nonatomic, strong) UIButton *backBtn;
@property (nonatomic, strong) UILabel *feedTitle;


@end

@implementation AICommonWebController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

- (void)viewWillDisappear:(BOOL)animated {
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    [super viewWillDisappear:animated];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorHex:@"#F5F5F5"];
    
    [self.view addSubview:self.backBtn];
    [self.view addSubview:self.feedTitle];
    [self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(PTW(44));
        make.leading.mas_equalTo(self.view).mas_offset(PTW(10));
        make.top.mas_equalTo(self.view).mas_offset(KStatusBarHeight);
    }];
    [self.feedTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.centerY.mas_equalTo(self.backBtn);
    }];
    
    [self.view addSubview:self.webView];
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.mas_equalTo(self.view);
        make.top.mas_equalTo(self.backBtn.mas_bottom);
    }];
    self.feedTitle.text = self.isTerms ? @"Terms of Service":@"Privacy Policy";
    NSString *urlString =  self.isTerms ? kTermsService:kPrivacyPolicy;
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
    [self.webView loadRequest:request];
    
}

#pragma mark -------------- lazy -----------
- (UIButton *)backBtn{
    if(!_backBtn){
        _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backBtn setImage:KImaged(@"ai_setting_back") forState:UIControlStateNormal];
        [_backBtn addTarget:self action:@selector(fanhuiAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backBtn;
}

- (void)fanhuiAction{
    [self.navigationController popViewControllerAnimated:YES];
}

- (UILabel *)feedTitle{
    if (!_feedTitle){
        _feedTitle = [UILabel new];
        _feedTitle.font = [UIFont fontWithWeight:Medium size:PTW(17)];
        _feedTitle.textColor = [UIColor colorHex:@"#333333"];
    }
    return _feedTitle;
}

#pragma mark---- 懒加载 ----
- (WKWebView *)webView{
    if(_webView == nil){
        WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
        configuration.preferences = [[WKPreferences alloc] init];
        //音频自动播放
        configuration.allowsInlineMediaPlayback = YES;
        configuration.mediaTypesRequiringUserActionForPlayback = false;
        configuration.processPool = [[WKProcessPool alloc] init];
        WKUserContentController *userContentController = [[WKUserContentController alloc] init];
        configuration.userContentController = userContentController;
        if ([configuration respondsToSelector:@selector(setDataDetectorTypes:)]) {
            configuration.dataDetectorTypes = UIDataDetectorTypeNone;
        }
        _webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration];
//        webView.UIDelegate = self;
        _webView.navigationDelegate = self;
        _webView.backgroundColor = [UIColor clearColor];
        _webView.opaque = NO;
        if (@available(iOS 11.0, *)) {
            _webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        [_webView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
    }
    return _webView;
}

#pragma mark --> WKWebView的代理方法

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    decisionHandler(WKNavigationActionPolicyAllow);
}

- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
   
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
   
}

- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
  
}

- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
   
}


@end
